package kr.pe.johnkim.carrotpoint.mobile.dao;

import kr.pe.johnkim.carrotpoint.mobile.domain.CarrotPointMerchant;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Carrot Point 가맹점 관련 dao 클래스 이다.
 *
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 2. 6.
 */
@Repository
public class CarrotPointMerchantDaoImpl extends SqlSessionDaoSupport implements CarrotPointMerchantDao {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(CarrotPointMerchantDaoImpl.class);

    /**
     * 최초 등록 시 가맹점 아이디로 중복 검사를 한다.
     *
     * @param carrotPointMerchant
     * @return
     */
    @Override
    public CarrotPointMerchant getMerchantExistById(CarrotPointMerchant carrotPointMerchant) {
        logger.info("getMerchantExistById");

        logger.info("merchantId : {}", carrotPointMerchant.getMerchantId());

        return (CarrotPointMerchant)getSqlSession().selectOne("CARROTPOINT_MERCHANT.getMerchantExistById", carrotPointMerchant);
    }

    /**
     * 가맹점을 등록 한다.
     *
     * @param carrotPointMerchant
     */
    @Override
    public void registerMerchant(CarrotPointMerchant carrotPointMerchant) {
        logger.info("registerMerchant");
        this.getSqlSession().insert("CARROTPOINT_MERCHANT.registerMerchant", carrotPointMerchant);
    }

    /**
     * 서버 로그인을 한다.
     *
     * @param carrotPointMerchant
     * @return
     */
    @Override
    public CarrotPointMerchant login(CarrotPointMerchant carrotPointMerchant) {
        logger.info("login");

        logger.info("merchantId : {}", carrotPointMerchant.getMerchantId());
//        logger.info("password   : {}", carrotPointMerchant.getPassword());

        return (CarrotPointMerchant)getSqlSession().selectOne("CARROTPOINT_MERCHANT.login", carrotPointMerchant);
    }

    /**
     * 가맹점 정보를 가져 온다.
     *
     * @param carrotPointMerchant
     * @return
     */
    @Override
    public CarrotPointMerchant getMerchantInformation(CarrotPointMerchant carrotPointMerchant) {
        logger.info("getMerchantInformation");
        return (CarrotPointMerchant)getSqlSession().selectOne("CARROTPOINT_MERCHANT.getMerchantInformation", carrotPointMerchant);
    }

    /**
     * 가맹점 정보를 수정 한다.
     *
     * @param carrotPointMerchant
     */
    @Override
    public void updateMerchantInformation(CarrotPointMerchant carrotPointMerchant) {
        logger.info("updateMerchantInformation");
        this.getSqlSession().update("CARROTPOINT_MERCHANT.updateMerchantInformation", carrotPointMerchant);
    }

    /**
     * 가맹점의 서비스 사용 제한 여부를 업데이트 한다.
     *
     * @param carrotPointMerchant
     */
    @Override
    public void updateRestrictionMerchant(CarrotPointMerchant carrotPointMerchant) {
        logger.info("updateRestrictionMerchant");
        this.getSqlSession().update("CARROTPOINT_MERCHANT.updateRestrictionMerchant", carrotPointMerchant);
    }

    /**
     * 가맹점의 위치 정보를 업데이트 한다.
     *
     * @param carrotPointMerchant
     */
    @Override
    public void updateMerchantLocation(CarrotPointMerchant carrotPointMerchant) {
        logger.info("updateMerchantLocation");

        logger.info("latitude  : {}", carrotPointMerchant.getLatitude());
        logger.info("longitude : {}", carrotPointMerchant.getLongitude());

        this.getSqlSession().update("CARROTPOINT_MERCHANT.updateMerchantLocation", carrotPointMerchant);
    }

    /**
     * 가맹점 검색 목록을 가져 온다.
     * 상호 명과 업종으로 검색 한다.
     *
     * @param carrotPointMerchant
     * @return
     */
    @Override
    public List<CarrotPointMerchant> getSearchMerchantList(CarrotPointMerchant carrotPointMerchant) {
        logger.info("getSearchMerchantList");

        logger.info("company name  : {}", carrotPointMerchant.getCompanyName());
        logger.info("business kind : {}", carrotPointMerchant.getBusinessKind01());
        logger.info("activate yn   : {}", carrotPointMerchant.getActivateYn());

        return this.getSqlSession().selectList("CARROTPOINT_MERCHANT.getSearchMerchantList", carrotPointMerchant);
    }

    /**
     * 가맹점 활성화 여부를 업데이트 한다.
     *
     * @param carrotPointMerchant
     */
    @Override
    public void updateMerchantActivateYn(CarrotPointMerchant carrotPointMerchant) {
        logger.info("updateMerchantActivateYn");
        this.getSqlSession().update("CARROTPOINT_MERCHANT.updateMerchantActivateYn", carrotPointMerchant);
    }
}
